home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13126 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  84 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Reading stdin twice
  5. Date: Thu, 04 Apr 96 18:34:40 GMT
  6. Organization: none
  7. Message-ID: <828642880snz@genesis.demon.co.uk>
  8. References: <4jsojd$1hlk@news.missouri.edu> <31628AEC.ABD@wight.hursley.ibm.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <31628AEC.ABD@wight.hursley.ibm.com>
  15.            dwater@hursley.ibm.com "Max Waterman" writes:
  16.  
  17. >Try :
  18. >
  19. >rewind( stdin )
  20. >
  21. >in between the functions.
  22.  
  23. This won't work if the file doesn't support seeking e.g. it is connected
  24. to a device such as a keyboard which is quite likely with stdin.
  25.  
  26. >Max.
  27. >
  28. >James P. Cooper wrote:
  29. >> 
  30. >> Hi,
  31. >> 
  32. >> I'm writing a program, and I'd like two functions to read the stdin
  33. >> stream.  Is this possible?  I realize I could pass a pointer to the
  34. >> buffer after the first function fread's stdin, but this would break
  35. >> something else more serious.
  36. >> 
  37. >> Example of what I'm doing:
  38. >> 
  39. >> void read_first() {
  40. >>    char *input;
  41. >>    FILE *infile;
  42. >> 
  43. >>    length = 5000;
  44. >>    fread(input, sizeof(char), length, stdin);
  45. >> 
  46. >>    /*  Do stuff with data here... */
  47. >> }
  48. >> 
  49. >> void read_again() {
  50. >>    char *input;
  51. >>    FILE *infile;
  52. >> 
  53. >>    length=5000;
  54. >>    fread(input,sizeof(char),length,stdin);
  55. >> 
  56. >>    /* Do something different  */
  57. >> }
  58. >> 
  59. >> I've tried to use fwrite to rewrite the buffer back to stdin, but that
  60. >> didn't work (presumably because stdin is read-only).
  61. >> 
  62. >> Any ideas?
  63.  
  64. What you need is a routine that reads a buffer and then passes that buffer
  65. to each function in turn. If you have to scan the input data twice in sequence
  66. then:
  67.  
  68. 1. you can do it directly with rewind(stdin) if the input file supports
  69.    seeking
  70.  
  71. 2. You could store the contents of the entire input in memory as it is
  72.    being scanned by the first funciton. The second function just scans the
  73.    memory image.
  74.  
  75. 3. You could copy the input out to a temporary file as it is being scanned
  76.    by the first function. The second function rescans the temporary file.
  77.    tmpfile() might be useful here.
  78.  
  79. -- 
  80. -----------------------------------------
  81. Lawrence Kirby | fred@genesis.demon.co.uk
  82. Wilts, England | 70734.126@compuserve.com
  83. -----------------------------------------
  84.